<?php

namespace app\unify\job;

use think\facade\Log;
use think\queue\Job;

class Event
{
    public function fire(Job $job, $data)
    {
        try {
            //信息
            Log::info(json_encode($data, JSON_UNESCAPED_UNICODE));

            //调用事件
            event($data['event'], $data['payload']);

            if (isset($data['async']['interval'])) {
                //删除并重新发起
                $job->release($data['async']['interval']);
            } else {
                //删除任务
                $job->delete();
            }
        } catch (\Exception $e) {
            if (in_array($job->attempts(), [1, 10, 100, 1000, 10000, 86400, $job->maxTries()])) {
                //失败次数，阶段性记录到日志中
                Log::error('当前已经失败：{n}次', [
                    'n' => $job->attempts(),
                ]);
                //错误内容
                Log::error($e->getTraceAsString());
            }
        }
    }

    /**
     * @Notes 执行失败
     * @Author EdwardCho
     * @Date 2022/5/18
     * @Time 14:04
     * @Interface failed
     * @param $data
     */
    public function failed($data)
    {
//        //记录错误日志
//        Log::error(json_encode($data, JSON_UNESCAPED_UNICODE));
    }
}